package com.cmcc.passenger.ui.profile

import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.cmcc.passenger.R
import com.cmcc.passenger.config.PassPath
import com.cmcc.passenger.databinding.ActivityProfileBinding
import com.cmcc.passenger.ui.base.BasicActivity
import com.cmcc.passenger.ui.fragment.AppAlertDialog
import com.cmcc.passenger.ui.fragment.AppProgressDialog
import com.cmcc.passenger.ui.profile.viewmodel.ProfileViewModel
import com.cmcc.shared.ActivityController
import com.cmcc.shared.SdkManager
import com.cmcc.shared.utils.PassDialogUtils
import com.cmcc.shared.utils.clickWithTrigger
import com.cmcc.shared.utils.showToast

import java.util.*

@Route(path = PassPath.PROFILE)
class ProfileActivity : BasicActivity(), ProfileMenuAdapter.ItemCliclListener {

    private lateinit var binding: ActivityProfileBinding
    private lateinit var viewModel: ProfileViewModel
    private var progressDialog: AppProgressDialog? = null
    override fun initialize(savedInstanceState: Bundle?) {
        viewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
        progressDialog = AppProgressDialog(this)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_profile)
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.toolbar.title.text = getString(R.string.passenger_personal_center)
        binding.toolbar.toolbar.setNavigationOnClickListener { finish() }

        var adapter = ProfileMenuAdapter(ProfileMenuAdapter.SingleItemDiffCallback(), this)
        binding.recyclerView.adapter = adapter
        val menus = ArrayList<MenuModel>()
        menus.add(MenuModel(getString(R.string.passenger_personal_accent), ""))
        menus.add(MenuModel(getString(R.string.passenger_personal_order), ""))
        menus.add(MenuModel(getString(R.string.passenger_personal_address), ""))
        menus.add(MenuModel(getString(R.string.sys_msg), ""))
       // menus.add(MenuModel(getString(R.string.passenger_apply), ""))

        menus.add(MenuModel(getString(R.string.str_update_time), "20200424"))
        adapter.submitList(menus)

        viewModel.getPassengerInfo()

        binding.passContactUs.contact.clickWithTrigger {
            AppAlertDialog(this).setContent(getString(R.string.str_if_you_need_call))
                .setNavigation(getString(R.string.cancel), null)
                .setPositive(getString(R.string.make_sure),
                    View.OnClickListener {
                        val data = Uri.parse("tel:85262325253 ")
                        val intent = Intent(Intent.ACTION_DIAL, data)
                        startActivity(intent)
                    }).show()
        }



    }

    override fun initObserver(savedInstanceState: Bundle?) {
        super.initObserver(savedInstanceState)

        viewModel?.passengerInfo?.observe(this, Observer { info ->
            var adapter = ProfileMenuAdapter(ProfileMenuAdapter.SingleItemDiffCallback(), this)
            binding.recyclerView.adapter = adapter
            val menus = ArrayList<MenuModel>()
            menus.add(MenuModel(getString(R.string.passenger_personal_accent), info.phone))
            menus.add(MenuModel(getString(R.string.passenger_personal_order), ""))
            menus.add(MenuModel(getString(R.string.passenger_personal_address), ""))
            menus.add(MenuModel(getString(R.string.sys_msg), ""))
           // menus.add(MenuModel(getString(R.string.passenger_apply), " "))
            menus.add(MenuModel(getString(R.string.str_update_time), "20200424"))
            adapter.submitList(menus)
        })



        viewModel.info.observe(this, Observer { info ->
            progressDialog!!.dismiss()
            Log.e(TAG, "---------------------" + info)
            if (info.code == 200)
            {
                //未提交
                if (info.data == null) {

                    ARouter.getInstance().build(PassPath.DRIVER_JOIN)
                        .navigation()
                }
                //var reviewStatus: String,//": "",         //审查状态(1-通过 2-待审核 3-失败)
                else {
                    if ("1".equals(info.data.reviewStatus)) {
                        reviewStatusSuccess()
                    }
                    if ("2".equals(info.data.reviewStatus)) {
                        reviewStatusIng()
                    }
                    if ("3".equals(info.data.reviewStatus)) {
                        reviewStatusFailuer()
                    }
                }
            } else {
                showToast(this, info.msg)
            }


        })
    }

    override fun cilick(position: Int) {
        if (position == 1) {
            //我的订单
            ARouter.getInstance().build(PassPath.MY_ORDER).navigation()
        }

        if (position == 2) {
            //常用地址管理
            ARouter.getInstance().build(PassPath.FAVORITE_ADDRESS)
                .navigation()
        }
        //系統消息
        if (position == 3) {

            ARouter.getInstance().build(PassPath.MSG_LIST)
                .navigation()

        }
        //申请加盟司机
        if (position == 4) {
            progressDialog?.show()
            getDiviceInfo()
        }

    }

    /**
     * 获取司机信息
     */
    private fun getDiviceInfo() {
        //获取司机审核状态
        viewModel.info()

    }

    override fun onDestroy() {
        super.onDestroy()
    }

    /**
     * 审核ing
     */
    private fun reviewStatusIng() {
        var dialog: Dialog =
            PassDialogUtils.getDialog(
                this,
                R.layout.dialog_regis_ing
            );
        dialog.show()

    }

    /**
     * 审核成功
     */
    private fun reviewStatusSuccess() {
        var dialog: Dialog =
            PassDialogUtils.getDialog(
                this,
                R.layout.dialog_regis_success
            )
        dialog.show()

        dialog.findViewById<View>(R.id.tv_ok).setOnClickListener(View.OnClickListener {
            dialog.dismiss()
            //清除token
            SdkManager.setMyLinkToken("")
            ActivityController.getInstance().finishAll()

        })


    }


    /**
     * 审核失败
     */
    private fun reviewStatusFailuer() {
        var dialog: Dialog =
            PassDialogUtils.getDialog(
                this,
                R.layout.dialog_regis_failuer
            );
        dialog.findViewById<View>(R.id.iv_cancel).setOnClickListener(View.OnClickListener {
            dialog.dismiss()

        })

        dialog.findViewById<View>(R.id.tv_input).setOnClickListener(View.OnClickListener {
            dialog.dismiss()
            ARouter.getInstance().build(PassPath.DRIVER_JOIN)
                .navigation()

        })
        dialog.show()
    }





}
